package com.xixing.weixin.cmd;

import com.xixing.weixin.exception.AccountException;
import com.xixing.weixin.exception.WeixinExecuteException;
import com.xixing.weixin.domain.Weixin;
import com.xixing.weixin.response.Response;

/**
 * Created with IntelliJ IDEA.
 * User: Apple
 * Date: 13-4-7
 * Time: 下午5:33
 * To change this template use File | Settings | File Templates.
 */
public class Command {
    private Executor command;
    private Object args;


    public Command(Executor command) {
        this.command = command;
    }

    public Response execute(CommandFactory commandFactory, Weixin weixin) throws WeixinExecuteException {
        Response response;
        try {
            Object args = command.parseArgs(weixin, this.args);
            response = command.execute(weixin, args);
            this.args = args;
        } catch (AccountException e) {
            command = commandFactory.getLoginCommand();
            args = new LoginExecutor.Arg(e.getMessage());
            response = command.execute(weixin, args);
        } catch (WeixinExecuteException e) {
            return command.help(weixin, this.args);
        }
        return response;
    }


}
